// firstPackage/pages/findHouseList/findHouseList.js

import { submitAiFindHouse } from "../../../service/house_service"
import { attentionAction } from "../../../service/attention_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    formData: {},
    houseList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      formData: app.globalData.aiData
    })
    this.submitAiFindHouse();
  },

  //提交ai找房请求:
  submitAiFindHouse() {
    wx.showLoading();
    submitAiFindHouse(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:找房结果:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          houseList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "找房失败",
        icon: "none"
      })
      console.error("JJLog:submitAiFindHouse", err);
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //点赞操作:
  collectAction(e) {
    let { data, index } = e.detail;
    this.attentionAction(data, index);
  },

  //关注操作
  attentionAction(item, index) {
    let data = {
      collectType: 1,
      number: item.houseNumber
    }
    wx.showLoading();
    attentionAction(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        item.isCollect = !item.isCollect;
        //动态数组:
        let changeItem = 'houseList[' + index + '].isCollect';
        this.setData({
          [changeItem]: item.isCollect,
        })
        wx.showToast({
          title: item.isCollect ? "关注成功" : "取关成功",
          icon: "none"
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:attentionAction", err);
    })
  },




})